package com.masiyi.easyexcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.masiyi.easyexcel.doman.Student;
import com.masiyi.easyexcel.service.StudentReadListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class EasyExcelController {

    /**
     * 读
     * @return
     */
    @GetMapping("/read")
    public List<Student> read() {
        String url = "/Users/wangfugui/IdeaProjects/demo/easy-excel-quickstart/学生信息.xlsx";
        ExcelReaderBuilder read = EasyExcel.read(url, Student.class, new StudentReadListener());
        ExcelReaderSheetBuilder sheet = read.sheet();
        return sheet.doReadSync();
    }

    /**
     * 写
     * @return
     */
    @GetMapping("/write")
    public String write() {
        String url = "/Users/wangfugui/IdeaProjects/demo/easy-excel-quickstart/学生信息" + UUID.randomUUID() + ".xlsx";
        ExcelWriterBuilder write = EasyExcel.write(url, Student.class);
        ExcelWriterSheetBuilder sheet = write.sheet();

        sheet.doWrite(initData());
        return "写入成功";
    }

    /**
     * 初始化数据
     * @return
     */
    private List<Student> initData() {
        ArrayList<Student> students = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            students.add(new Student(i, "张" + i, i, new Date()));
        }
        return students;
    }

    /**
     * 导出
     * @param response
     * @throws Exception
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {

        response.setHeader("Content-disposition", "attachment; filename=" + new String("学生信息".getBytes("gb2312"), "ISO8859-1") + ".xlsx");

        ServletOutputStream out;

        out = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(out, Student.class);
        ExcelWriterSheetBuilder sheet = write.sheet();

        sheet.doWrite(initData());

        out.flush();
        response.getOutputStream().close();
        out.close();

    }


    /**
     * 导入
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public List<Student> upload(MultipartFile multipartFile) throws IOException {

        ExcelReaderBuilder read = EasyExcel.read(multipartFile.getInputStream(), Student.class, new StudentReadListener());
        ExcelReaderSheetBuilder sheet = read.sheet();
        return sheet.doReadSync();
    }

}
